﻿/*+===================================================================
File:      Bullet.cpp

Summary:   Hiện thực hóa các phương thức của lớp CBridge
===================================================================+*/

#include "Bridge.h"

CBridge::CBridge(CBill* bill, D3DXVECTOR2 pos, int id, int typeID)
	:CMoveableObject(pos, id, typeID)
{
	m_pResourceManager->GetSprite(ID_SPRITE_BRIDGE1);
	m_pResourceManager->GetSprite(ID_SPRITE_BRIDGE2);
	m_pResourceManager->GetSprite(ID_SPRITE_BRIDGE3);
	m_pResourceManager->GetSprite(ID_SPRITE_BRIDGE4);
	m_pResourceManager->GetSprite(ID_SPRITE_BRIDGE5);
	this->m_bill = bill;
	m_nStatus = ORIGINAL;
	m_pSprite = m_pResourceManager->GetSprite(ID_SPRITE_BRIDGE1);
	m_v = D3DXVECTOR2(0, 0);
	m_bIsStartExplosion = false;
	m_bIsLive = true;
	m_fTimeBridge = 0.0f;
	m_fTimeChange = 0.0f;
	exCount = 0;
	for (int i = 0; i < 4; i++)
	{
		m_aExSrpite[i] = new CSprite(*m_pResourceManager->GetSprite(ID_SPRITE_BIG_EXPLOSION));
	}
}

CBridge::~CBridge()
{
	if (m_bill)
		delete m_bill;
	for (int i = 0; i < 4; i++)
	{
		delete m_aExSrpite[i];
	}
}

void CBridge::Update(float timeFrame)
{
	if (!m_bIsLive)
		return;
	D3DXVECTOR2 billPos = m_bill->GetPos();
	CExplosion* tmpExplosion = NULL;
	if (billPos.x >= m_pos.x - m_pSprite->GetWidth() / 2 + 10)
	{
		m_bIsStartExplosion = true;
	}

	m_pSprite->Update(timeFrame);
	if (m_fTimeChange > 0.0f)
	{
		m_fTimeChange -= timeFrame;
	}
	else
	{		
		if (m_bIsStartExplosion)
		{
			m_nStatus = m_nStatus + 1;
			m_fTimeChange = 52 * timeFrame;
			exCount = 0;
			m_pSprite = m_pResourceManager->GetSprite(SPRITE_ID[m_nStatus]);
			if (m_nStatus >= FINISH)
			{
				m_bIsStartExplosion = false;
				this->Kill();
			}
			UpdateBox(timeFrame);
		}
	}
	if (m_fTimeBridge > 0.0f)
	{
		m_fTimeBridge -= timeFrame;
	}
	else
	{
		if (m_nStatus > ORIGINAL && m_nStatus < FINISH)
		{
			switch (exCount)
			{
			case 0:
				m_fTimeBridge = 0.0f;
				tmpExplosion = new CExplosion(D3DXVECTOR2(m_box.x - 64.0f, m_box.y - 31.0f), 0,
					ID_BIG_EXPLOSION, 0, 41 * timeFrame);
				break;
			case 1:
				m_fTimeBridge = 0.048f;
				tmpExplosion = new CExplosion(D3DXVECTOR2(m_box.x - 32.0f, m_box.y - 31.0f),
					0, ID_BIG_EXPLOSION, 0, 41 * timeFrame);
				break;
			case 2:
				m_fTimeBridge = 0.064f;
				tmpExplosion = new CExplosion(D3DXVECTOR2(m_box.x - 32.0f, m_box.y),
					0, ID_BIG_EXPLOSION, 0, 41 * timeFrame);
				break;
			case 3:
				m_fTimeBridge = 0.08f;
				tmpExplosion = new CExplosion(D3DXVECTOR2(m_box.x, m_box.y - 31.0f),
					0, ID_BIG_EXPLOSION, 0, 41 * timeFrame);
				break;
			}
			if (exCount <= 3)
			{
				CPlayState::s_objsOutSideQuadTree.push_back(tmpExplosion);
			}
			exCount = exCount++;
		}
	}
}

void CBridge::UpdateBox(float timeFrame)
{
	float xChange = m_pSprite->GetWidth() * 0.25f;
	if (m_nStatus > ORIGINAL)
	{
		m_box.x += xChange;
		m_box.width -= xChange;
	}
}

void CBridge::Draw()
{
	if (!m_bIsLive)
		return;
	D3DXVECTOR2 pos = CViewport::GetInstance()->TransformMatrix(m_pos);
	m_pSprite->Draw(pos);
}